package com.fone.player.online.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.fone.player.R;
import com.fone.player.activity.BaseActivity;
import com.fone.player.client.Callback;
import com.fone.player.client.Error;
import com.fone.player.client.Request;
import com.fone.player.client.TopspotRst;
import com.fone.player.online.adapter.LeaderboardColumnAdapter;
import com.fone.player.util.FoneUtil;
import com.fone.player.util.L;
import com.fone.player.widget.xlistview.XListView;
import com.fone.player.widget.xlistview.XListView.IXListViewListener;

public class LeaderboardColumnActivity extends BaseActivity implements IXListViewListener, OnClickListener {

	private static final String TAG = "LeaderboardColumnActivity";
	
	private Button mLeftBt;
	private Button mRightBt;
	private TextView mTitleTv;
	
	private View empty;
	private View waitting;
	private View waittingProgress;
	
	private XListView mListView;
	private LeaderboardColumnAdapter mAdapter;
	
	private String shost;
	private String host;
	private String url;
	private String name;
	private String id;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub 
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_leaderboard_column);
		getIntentData();
		iniView();
		iniData();
	}
	
	private void getIntentData(){
		Intent intent = getIntent();
		url = intent.getStringExtra("url");
		name = intent.getStringExtra("name");
		id = intent.getStringExtra("id");
		
	}
	
	private void iniView(){
		
		//title
		mLeftBt = (Button) findViewById(R.id.common_title_left_bt);
		mRightBt = (Button) findViewById(R.id.common_title_right_bt);
		mTitleTv = (TextView) findViewById(R.id.common_left_title_tv);
		mRightBt.setVisibility(View.GONE);
		mLeftBt.setOnClickListener(this);
		
		mTitleTv.setText(name);
		mTitleTv.setVisibility(View.VISIBLE);
		
		//body
		mListView = (XListView) findViewById(R.leaderboard.leaderboard_column_list);
		mListView.setPullLoadEnable(false);
		mListView.setPullRefreshEnable(true);
		mListView.setEnableDragLoadMore(false);
		mListView.setXListViewListener(this);
		mAdapter = new LeaderboardColumnAdapter(this, shost, host);
		mListView.setAdapter(mAdapter, true);
		
		empty = findViewById(R.id.empty);
		
		waitting = findViewById(R.id.leaderboard_waitting_ll);
		waittingProgress = waitting.findViewById(R.id.leaderboard_progress);
	}
	
	private void iniData(){
		//TODO  初始化数据  请求网络数据
		
//		url="[host]/player/specllist?tmpid=15&ifp=0&clid=52503";
//		url="[host]/player/topspot?clid=52502&actionpath=30%7C0";
		
		showWaitting(true);
		Request.getInstance().topspot(FoneUtil.onlineUrl(url), new TopspotCallBack());
		L.v(TAG, "iniData", FoneUtil.onlineUrl(url));
		
//		fillData();
	}

	/**
	 * 填充数据
	 */
	private void fillData(TopspotRst t){
		
		mListView.stopRefresh();
		mListView.stopLoadMore();

		//TODO 填充数据
		shost = t.shost;
		host = t.host;

		if(t.modules!=null){
			mAdapter.initData(t.modules.moduleList,shost,host);
			mAdapter.notifyDataSetChanged();
		}
		showWaitting(false);
		
	}
	
	/**
	 * 是否显示waitting
	 * @param showing
	 */
	private void showWaitting(boolean showing){
		if(showing){
			waitting.setVisibility(View.VISIBLE);
			waittingProgress.setVisibility(View.VISIBLE);
		}else{
			waittingProgress.setVisibility(View.GONE);
			waitting.setVisibility(View.GONE);
		}
	}
	
	@Override
	public void onRefresh() {
		mListView.onLoad();
		
		//TODO 请求数据刷新
		waitting.setVisibility(View.VISIBLE);
		Request.getInstance().topspot(FoneUtil.onlineUrl(url), new TopspotCallBack());
	}
	
	@Override
	public void onLoadMore() {}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.common_title_left_bt:
			finish();
			break;

		default:
			break;
		}
		
	}
	
	private class TopspotCallBack implements Callback<TopspotRst> {

		@Override
		public void onSuccess(TopspotRst t) {
			L.v(TAG, "onSuccess", "result : " + t.result);
			if(t.result!=0){
				mListView.stopRefresh();
				mListView.stopLoadMore();
				showWaitting(false);
				FoneUtil.showToast(mContext, "数据获取失败，请稍后重试");
				return;
			}
			fillData(t);
		}

		@Override
		public void onFailure(Error error) {
			// TODO Auto-generated method stub
			mListView.stopRefresh();
			mListView.stopLoadMore();
			showWaitting(false);
			L.v(TAG, "onFailure", "error : " + error.toString());
			if(error.isNetworkError()){
				FoneUtil.showToast(mContext, "网络超时，请稍后重试");
				return;
			}
				FoneUtil.showToast(mContext, "数据获取失败，请稍后重试");
		}
		
	}

}
